package com.manor.one.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.manor.common.Constants;
import com.manor.common.dialog.AbsDialogFragment;
import com.manor.common.utils.DpUtil;
import com.manor.common.utils.StringUtil;
import com.manor.one.R;

import androidx.fragment.app.FragmentManager;

/**
 * 守护弹出框

 * @version 1.0

 * @date 2020/4/11  22:54
 */

public class GuardDialogFragment extends AbsDialogFragment implements View.OnClickListener {

    private boolean mCancelable;
    private TextView mTvText;
    private ImageView mIvImage;

    private String mImgUrl = "";
    private String mContent = "";
    private ActionConfirmListener mActionListener;

    public static void forward(FragmentManager manager, String url, String content,ActionConfirmListener listener){
        Bundle bundle = new Bundle();
        bundle.putString(Constants.URL,url);
        bundle.putString(Constants.TEXT_CONTENT,content);

        GuardDialogFragment fragment = new GuardDialogFragment();
        fragment.setArguments(bundle);
        fragment.setCancelable(true);
        fragment.setActionListener(listener);
        fragment.show(manager, "SignDialogFragment");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_guard;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.dialog_sd;
    }

    @Override
    protected boolean canCancel() {
        return mCancelable;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        WindowManager.LayoutParams params = window.getAttributes();
//        params.width = DpUtil.dp2px(220);
//        params.height = DpUtil.dp2px(140);
        params.y = DpUtil.dp2px(36);
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle arguments = getArguments();
        mImgUrl = arguments.getString(Constants.URL,"");
        mContent = arguments.getString(Constants.TEXT_CONTENT,"");

        mTvText = (TextView) findViewById(R.id.textView6);
        mIvImage = (ImageView) findViewById(R.id.iv_img);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        if (mCancelable) {
            findViewById(R.id.btn_close).setOnClickListener(this);
        } else {
            findViewById(R.id.btn_close).setVisibility(View.INVISIBLE);
            Dialog dialog = getDialog();
            if (dialog != null) {
                dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                            return true;
                        }
                        return false;
                    }
                });
            }
        }
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        setDataInfo();
    }




    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_close || i == R.id.btn_cancel) {
            dismiss();
        } else if (i == R.id.btn_confirm) {
            confirm();
        }
    }

    private void setDataInfo(){
        Glide.with(getDialog().getContext())
                .load(mImgUrl)
                .thumbnail(Glide.with(getDialog().getContext()).load(R.mipmap.icon_circle_default_a))
                .apply(new RequestOptions().transform(new CenterCrop(), new CircleCrop()))
                .into(mIvImage);
        SpanUtils.with(mTvText)
                .setForegroundColor(Color.parseColor("#FFFFFFFF"))
                .setFontSize(14, true)
                .append("赠送")
                .append("【"+mContent+"】")
                .setBold()
                .append("糖果以上的礼物就能守护TA拉近彼此的关系,")
                .append("解锁语视音特权。")
                .setBold()
                .create();
    }



    private void confirm() {
        if (StringUtil.isNotEmpty(mContent)){
            if (mActionListener!=null) {
                dismiss();
                mActionListener.onConfirm(mContent);
            }
        }
    }


    @Override
    public void setCancelable(boolean cancelable) {
        mCancelable = cancelable;
    }


    @Override
    public void onDestroy() {
        mContext = null;
        mActionListener = null;
        super.onDestroy();
    }

    public interface ActionConfirmListener{
        void onConfirm(String money);
    }


    public void setActionListener(ActionConfirmListener actionListener) {
        mActionListener = actionListener;
    }



}
